﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2015/4/12
 * Time: 21:58
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Net;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;

namespace Look52pojie
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		const string MyTitle = "吾爱破解论坛开放注册信息提取工具";
		
		public MainForm(bool bAuto)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			if (bAuto)
				BtnSendClick(btnSend, new EventArgs());
		}
		
		void BtnSendClick(object sender, EventArgs e)
		{
			btnSend.Enabled = false;
			try {
				GetTopics(txtUrl.Text);
			} catch (Exception ex) {
				MessageBox.Show(ex.Message, MyTitle);
			}
			btnSend.Enabled = true;
		}
		
		void GetTopics(string url)
		{
			double nearestDays = 365.0;
			WebRequest wr = HttpWebRequest.Create(url);
			WebResponse res = wr.GetResponse();
			StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("gbk"));
			string resStr = sr.ReadToEnd();
			sr.Close();
			txtSourceCode.Text = resStr;
			Regex re = new Regex(@"<a href=""(http://www.52pojie.cn/thread-\d+-1-1.html)"" onclick=""atarget\(this\)"" class=""s xst"">(.+?)</a>", RegexOptions.Multiline);
			MatchCollection mc = re.Matches(resStr);
//			MessageBox.Show("找到" + mc.Count.ToString() + "个帖子\r\n" + @"<tbody id=""normalthread_\d+"">(.+)</tbody>");
			
			List<Topic> topics = new List<Topic>();
			List<Topic> regTopics = new List<Topic>();
			foreach (Match m in mc)
			{
				GroupCollection gc = m.Groups;
				string sUrl = gc[1].Value;
				string sTitle = gc[2].Value;
//				lstTopic.Items.Add(sTitle);
				topics.Add(new Topic(sTitle, sUrl));
				if (sTitle.Contains("【开放注册公告】"))
				{
//					lstOpenRegist.Items.Add(sTitle);
					regTopics.Add(new Topic(sTitle, sUrl));
					Regex r1 = new Regex(@"(\d+年\d+月\d+日)");
					Regex r2 = new Regex(@"(\d+)年.*?([暑|寒])假");
					Regex r3 = new Regex(@"(\d+)年.*?光棍节");
					Match mm = r1.Match(sTitle);
					string retDate = "";
					if (mm.Success)
					{
						retDate = mm.Groups[1].Value;
					}
					else
					{
						mm = r2.Match(sTitle);
						if (mm.Success)
						{
							retDate = mm.Groups[1].Value;
							if (mm.Groups[2].Value.Equals("暑"))
								retDate += "年7月7日";
							else
								retDate += "年2月1日";
						}
						else
						{
							mm = r3.Match(sTitle);
							if (mm.Success)
								retDate = mm.Groups[1].Value + "年11月11日";
						}
					}
					
					if (retDate.Length > 0)
					{
						DateTime dtNew = DateTime.ParseExact(retDate, "yyyy年M月d日", null);
						DateTime dtNow = DateTime.Now;
//						TimeSpan tsNew = dtNew.Ticks;
//						TimeSpan tsNow = dtNow.Ticks;
//						TimeSpan ts = tsNew.Subtract(tsNow).Duration();
						TimeSpan ts = dtNew - dtNow;
						double dDays = ts.TotalDays;
						if (Math.Abs(dDays) < Math.Abs(nearestDays))
						{
							nearestDays = dDays;
						}
					}
				}
			}
			
			if (nearestDays < 365.0)
			{
				string sTit = MyTitle;
				if (nearestDays >= 1.0)
				{
					sTit += " (距下一次开放注册还有" + Math.Ceiling(nearestDays).ToString() + "天)";
				}
				else if (nearestDays < 0)
				{
					sTit += " (上次开放注册已过" + Math.Ceiling(Math.Abs(nearestDays)).ToString() + "天)";
				}
				else
				{
					sTit += " (今日开放注册！！！)";
				}
				this.Text = sTit;
			}
			
			// ListBox控件实现
			lstTopic.DataSource = topics;		// 指定数据源
			lstTopic.DisplayMember = "Title";	// 界面显示的属性
			
			lstOpenRegist.DataSource = regTopics;	// 指定数据源
			lstOpenRegist.DisplayMember = "Title";	// 界面显示的属性
		}
		
		void LstOpenRegistDrawItem(object sender, DrawItemEventArgs e)
		{
			e.DrawBackground();
			e.DrawFocusRectangle();
			
			// 让文字位于Item的中间
			float difH = (e.Bounds.Height - e.Font.Height) / 2;
			RectangleF rf = new RectangleF(e.Bounds.X, e.Bounds.Y + difH, e.Bounds.Width, e.Font.Height);
			e.Graphics.DrawString(lstOpenRegist.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), rf);
			
//			e.Graphics.DrawString(lstOpenRegist.Items[e.Index].ToString(), e.Font, new SolidBrush (e.ForeColor), e.Bounds);
		}
		
		void LstOpenRegistMeasureItem(object sender, MeasureItemEventArgs e)
		{
			e.ItemHeight = 22;
		}
		
		void LstOpenRegistDoubleClick(object sender, EventArgs e)
		{
			Topic t = (Topic)lstOpenRegist.SelectedItem;
			string sUrl = t.Url;
			// 调用系统默认的浏览器  
			System.Diagnostics.Process.Start(sUrl);
		}
		
		void LstTopicDrawItem(object sender, DrawItemEventArgs e)
		{
			e.DrawBackground();
			e.DrawFocusRectangle();
			
			//让文字位于Item的中间
			float difH = (e.Bounds.Height - e.Font.Height) / 2;
			RectangleF rf = new RectangleF(e.Bounds.X, e.Bounds.Y + difH, e.Bounds.Width, e.Font.Height);
			e.Graphics.DrawString(lstTopic.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), rf);
			
//			e.Graphics.DrawString(lstTopic.Items[e.Index].ToString(), e.Font, new SolidBrush (e.ForeColor), e.Bounds);
		}
		
		void LstTopicMeasureItem(object sender, MeasureItemEventArgs e)
		{
			e.ItemHeight = 22;
		}
		
		void LstTopicDoubleClick(object sender, EventArgs e)
		{
			Topic t = (Topic)lstTopic.SelectedItem;
			string sUrl = t.Url;
			// 调用系统默认的浏览器  
			System.Diagnostics.Process.Start(sUrl);
		}
	}
}
